//
//  GLLive_Helper.swift
//  GLUZSwift
//
//  Created by yu on 2025/3/28.
//

import Foundation

class GLLive_Helper {

    static func liveRoomId(userId: Int) -> String {
        "live_\(userId)_\(Date.sg.timestamp())"
    }
    
    static func openLiveStreamRoom(exhibitionId: Int) {
        let liveVC = liveStreamingVC(role: .host,  exhibitionId: exhibitionId, roomId: liveRoomId(userId: UserProfile.userId))
        if let vc = currentViewController() {
            vc.present(BaseNavigationController(rootViewController: liveVC))
        }
    }
    
    static func joinLiveSteamingRoom(exhibitionId: Int, hostId: String, roomId: String) {
        let liveVC = liveStreamingVC(role: .audience, exhibitionId: exhibitionId, hostId: hostId, roomId: roomId)
        if let vc = currentViewController() {
            vc.present(BaseNavigationController(rootViewController: liveVC))
        }
    }
    
    fileprivate static func liveStreamingVC(role: ZegoLiveStreamingRole, exhibitionId: Int, hostId: String? = nil, roomId: String) -> GLPrebuiltLiveStreamingVC {
        let appID = KeyCenter.IM.appID
        let appSign = KeyCenter.IM.appSign
        let userName = LanguageManager.isCountryCN ? UserProfile.username: UserProfile.usernameRu
        let userId = String(UserProfile.userId)
        
        let config: GLUIKitPrebuiltLiveStreamingConfig
        switch role {
        case .host:
            config = GLUIKitPrebuiltLiveStreamingConfig.gl_host(enableSignalingPlugin: true)
        default:
            config = GLUIKitPrebuiltLiveStreamingConfig.gl_audience(enableSignalingPlugin: true)
        }
        // 修改语言为中文
        config.translationText = ZegoLiveTranslationText(language: LanguageManager.isCountryCN ? .CHS: .RU)
        config.enableCoHosting = true
        config.userAvatarUrl = UserProfile.avatar
        
        let liveVC = GLPrebuiltLiveStreamingVC(
            appID,
            appSign: appSign,
            userID: userId,
            userName: userName,
            liveID: roomId,
            config: config,
            hostId: hostId ?? userId,
            exhibitionId: exhibitionId
        )
        
        liveVC.modalPresentationStyle = .fullScreen
        
        return liveVC
        
    }
    
    static var zego_userDict: ThreadSafeDictionary<String, ZIMKitUser> = .init()
    /// 获取机构的用户信息
    static func getUserInfo(userID:String, callback: QueryUserInfoCallback? = nil) {
        if let user = zego_userDict[userID] {
            callback?(user)
        } else {
            ZIMKitCore.shared.queryUserInfo(by: userID) { userInfo, error in
                if let userInfo = userInfo {
                    if userInfo.avatarUrl != nil {
                        zego_userDict[userID] = userInfo
                    }
                    callback?(userInfo)
                }
            }
        }
    }
    
}

extension GLLive_Helper {
    /// 预约 1v1
    static func appointment1V1(receiveUserId: Int, roomId: String? = nil) {
        YLHUD.showLottie()
        
        let target = JavaAPI.saveClockCallRecord(callUserId: UserProfile.userId, receiveUserId: receiveUserId, type: 2, roomId: roomId)
        YLRequest.request(target) { result in
            YLHUD.hideHUD()
            switch result {
            case .success( _):
                YLAlert(controller: currentViewController() ?? xKeyWindow.rootViewController!, message: "申请1v1会议成功".localized(.live), actionTitles: ["确定"], cancelTitle: nil)
            case .failure( _):
                break
            }
        }
    }
}
